import Vue from 'vue'
import App from './App'
import router from './router'
import validator from 'vue-validator'
import vueResource from 'vue-resource'
import "./databus/path"
import "./databus/tools"
import "./utils/jquery/jquery-3.1.1.min"


Vue.use(validator);
Vue.use(vueResource);
Vue.config.productionTip = false;

Vue.prototype.setCookie = function(name, value, expires){
	  var exdate = new Date();
	  exdate.setDate(exdate.getDate() + expires);
	  document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ";expires=" + exdate.toGMTString());
}

Vue.prototype.getCookie = function(name){
	var arrays ,reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if(arrays = document.cookie.match(reg)){
		return arrays[2];
	}
	return null;
}

Vue.prototype.delCookie = function(name){
	var expires = new Date();
	expires.setTime(expires.getTime() - 1);
	var value = this.getCookie(name);
	if(value != null){
		document.cookie = name + "=" + value + ";expires=" + expires.toGMTString();
	}
}

Vue.http.interceptors.push(function(request, next) {
	  request.headers.set('Authorization', 'Bearer '+sessionStorage.token);
	  next();
})

var vms = new Vue({
	el : "#app",
	router : router,
	template : "<App/>",
	components : {App},
	watch:{
		"$route":"checkLogin"
	},
	created(){
	    this.checkLogin();
	},
	methods:{
		checkLogin(){
		  let session = sessionStorage.getItem("user");
			if(!session){
				this.$router.push("/login");
			}
		}
	}
})

